<?php
class Share_media extends CI_Model {
    
    function __construct(){
        parent::__construct();
        $this->load->model('get_profile_infos');
        $this->load->model('manage_notifications');
        date_default_timezone_set('UTC');
        
    }


    function addVideo($videoID,$title,$desc,$thumb,$identifier,$hd,$uploaded,$timestamp) {
        
        $data = array(
          'video_id' => $videoID,
          'user_id' => $this->input->cookie('LEAPSESSID'),
          'title' => $title,
          'description' => $desc,
          'thumb' => $thumb,
          'identifier' => $identifier,
          'hd' => $hd,
          'uploaded' => $uploaded,
          'timestamp' => $timestamp,
        );
        var_dump($data);
        $insert = $this->mongo_db->insert('videos', $data);
        if($insert) {
            return $this->facebookOG("publish","http://leapdemo.com/videos/".$videoID,"parkour_video");
        }
        return $insert;
    }
    function getVideos(){
        $page = 0;
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->offset($page)->get('videos');
        return $query;
    }
    function getMainVideos(){
        $page = 20;
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->limit($page)->get('videos');
        foreach($query as $key => $value){
            $infos = $this->get_profile_infos->getInfosID($value['user_id']);
            $query[$key]['user_name'] = $infos['first_name']." ".$infos['last_name'];
            $query[$key]['user_thumb'] = $infos['profile_pic_thumb'];
        }
        return $query;
    }
    function getPopVideosHome(){
        $this->load->model('manage_comments');
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('videos');
        $poular = array();
        foreach($query as $key => $value){
            if ($key > 10)
                break;
            $likes = $this->getLikesVid($value['video_id']);
            $favs = $this->getFavsVid($value['video_id']);
            $comments = count($this->manage_comments->getcomments("video",$value['video_id']));
            $order = $comments + ($likes * 5) + ($favs * 10);
            if($order >= 15){
                array_push($poular, $query[$key]);
            }
        }
        return $poular;
    }
    function getPopVideos(){
        $this->load->model('manage_comments');
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('videos');
        $poular = array();
        foreach($query as $key => $value){
            $likes = $this->getLikesVid($value['video_id']);
            $favs = $this->getFavsVid($value['video_id']);
            $comments = count($this->manage_comments->getcomments("video",$value['video_id']));
            $order = $comments + ($likes * 5) + ($favs * 10);
            if($order >= 15){
                array_push($poular, $query[$key]);
            }
        }
        return $poular;
    }
    function getChoiceVideos(){
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('choice_vid');
        return $query;
    }
    function getFreshVideos(){
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('videos');
        $fresh = array();
        foreach($query as $key => $value){
            if(isset($value['timestamp']) && strtotime("-1 week") < $value['timestamp']){
                array_push($fresh, $query[$key]);
            }
        }
        return $fresh;
    }
    function getUpcomingVideos(){
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('videos');
        return $query;
    }
    function getFavVideos(){
        $data = array(
            'user_id' => $this->input->cookie('LEAPSESSID'),
        );
        $this->mongo_db->where($data);
        $query = $this->mongo_db->get('favorite_vid');
        $favs = array();
        foreach($query as $key => $value){
               $video = $query = $this->mongo_db->where("video_id",$value['video_id'])->get('videos');
               array_push($favs, $video['0']);
        }
        return $favs;
    }
    function getVideosFromID($user_id){
        $this->mongo_db->where('user_id', $user_id);
        $query = $this->mongo_db->order_by(array('timestamp' => 'DESC'))->get('videos');
        return $query;
    }
    function editVideoInfos(){
        
    }
    function rand_string( $length ) {
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str ='';
	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) {
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}

	return $str;
    }
    function addImage($photoID,$name,$source,$thumb,$width,$height,$uploaded,$timestamp){
        $data = array(
          'photo_id' => $photoID,
          'originale' => $name,
          'thumb' => $thumb,
          'user_id' => $this->input->cookie('LEAPSESSID'),
          'title' => "",
          'width' => $width,
          'height' => $height,
          'description' => "",
          'source' => $source,
          'photograph' => 'Unknown',
          'uploaded' => $uploaded,
          'timestamp' => $timestamp
        );
        $insert = $this->mongo_db->insert('photos', $data);
        if($insert) {
            return $this->facebookOG("publish","http://leapdemo.com/photos/".$photoID,"parkour_photo");
        }
    }
    function editImageInfos($photo_id,$title,$desc,$photograph){
        if(!$photograph || $photograph === ''){
            $photograph = "Unknown";
        }
        $data = array(
          'title' => $title,
          'description' => $desc,
          'photograph' => $photograph
        );
        $this->mongo_db->where('photo_id' , $photo_id);
        $query = $this->mongo_db->set($data)->update('photos');
        return $query;
    }

    function add500pxImage($id,$source,$thumb,$title,$desc,$photograph,$uploaded,$timestamp){
        if(!$photograph || $photograph === ''){
            $photograph = "Unknown";
        }
        $data = array(
          'photo_id' => $id,
          'user_id' => $this->input->cookie('LEAPSESSID'),
          'title' => $title,
          'thumb' => $thumb,
          'description' => $desc,
          'source' => $source,
          'photograph' => $photograph,
          'uploaded' => $uploaded,
          'timestamp' => $timestamp
        );
        $insert = $this->mongo_db->insert('photos', $data);
        if($insert){
            return 1;
        }
        else
            return false;
    }
    function getPhotos(){
        $query = $this->mongo_db->order_by(array('_id' => 'DESC'))->get('photos');
        return $query;
    }
    function getPhoto($photo_id){
        $this->mongo_db->where('photo_id' , $photo_id);
        $query = $this->mongo_db->get('photos');
        return $query;
    }
    function getVideo($video_id){
        $this->mongo_db->where('video_id' , $video_id);
        $query = $this->mongo_db->get('videos');
        return $query;
    }
    function getPhotosFromID($user_id){
        $this->mongo_db->where('user_id', $user_id);
        $query = $this->mongo_db->get('photos');
        return $query;
    }
    function likePhoto($photo_id,$user_id){
        $uploaded_at =  date('F\, l j Y');
        $timestamp = time();
        $data = array(
          'photo_id' => $photo_id,
          'user_id' => $user_id,
          'uploaded_at' => $uploaded_at,
          'timestamp' => $timestamp
        );
        $insert = $this->mongo_db->insert('likes_ph', $data);
        $photo = $this->getPhoto($photo_id);
        if($photo[0]['user_id'] !== $photo_id){
            $this->manage_notifications->manageNotification($user_id,$photo_id,$photo,$uploaded_at,$timestamp,'likes','photo');
        }
        if($insert){
           return $this->facebookOG("like","http://leapdemo.com/photos/".$photo_id,"parkour_photo");
        }
        return $insert;
    }
    function dislikePhoto($photo_id,$user_id){
        $data = array(
          'photo_id' => $photo_id,
          'user_id' => $user_id
        );
        $this->mongo_db->where($data);
        $dislike = $this->mongo_db->delete('likes_ph');
        return $dislike;
    }
    function likeVideo($video_id,$user_id){
        $uploaded_at =  date('F\, l j Y');
        $timestamp = time();
        $data = array(
          'video_id' => $video_id,
          'user_id' => $user_id,
          'uploaded_at' => $uploaded_at,
          'timestamp' => $timestamp
        );
        $insert = $this->mongo_db->insert('likes_vid', $data);
        $video = $this->getVideo($video_id);
        if($video[0]['user_id'] !== $video_id){
            $this->manage_notifications->manageNotification($user_id,$video_id,$video,$uploaded_at,$timestamp,'likes','video');
        }
        if($insert){
           return $this->facebookOG("like","http://leapdemo.com/videos/".$video_id,"parkour_video");
        }
        return $insert;
    }
    function dislikeVideo($video_id,$user_id){
        $data = array(
          'video_id' => $video_id,
          'user_id' => $user_id
        );
        $this->mongo_db->where($data);
        $dislike = $this->mongo_db->delete('likes_vid');
        return $dislike;
    }
    function favoritePhoto($photo_id,$user_id){
        $uploaded_at =  date('F\, l j Y');
        $timestamp = time();
        $data = array(
          'photo_id' => $photo_id,
          'user_id' => $user_id,
          'uploaded_at' => $uploaded_at,
          'timestamp' => $timestamp
           
            );
        $insert = $this->mongo_db->insert('favorite_ph', $data);
        $photo = $this->getPhoto($photo_id);
        if($photo[0]['user_id'] !== $photo_id){
            $this->manage_notifications->manageNotification($user_id,$photo_id,$photo,$uploaded_at,$timestamp,'faved','photo');
        }
        if($insert){
           return $this->facebookOG("fave","http://leapdemo.com/photos/".$photo_id,"parkour_photo");
        }
        return $insert;
    }
    function favoriteVideo($video_id,$user_id){
        $uploaded_at =  date('F\, l j Y');
        $timestamp = time();
        $data = array(
          'video_id' => $video_id,
          'user_id' => $user_id,
          'uploaded_at' => $uploaded_at,
          'timestamp' => $timestamp
        );
        $insert = $this->mongo_db->insert('favorite_vid', $data);
        $video = $this->getVideo($video_id);
        if($video[0]['user_id'] !== $video_id){
            $this->manage_notifications->manageNotification($user_id,$video_id,$video,$uploaded_at,$timestamp,'faved','video');
        }
        if($insert){
           return $this->facebookOG("fave","http://leapdemo.com/videos/".$video_id,"parkour_video");
        }
        return $insert;
    }
    function rmvFavorite($photo_id,$user_id){
        $data = array(
          'photo_id' => $photo_id,
          'user_id' => $user_id
        );
        $this->mongo_db->where($data);
        $delete = $this->mongo_db->delete('favorite_ph');
        return $delete;
    }
    function rmvFavoriteVid($video_id,$user_id){
        $data = array(
          'video_id' => $video_id,
          'user_id' => $user_id
        );
        $this->mongo_db->where($data);
        $delete = $this->mongo_db->delete('favorite_vid');
        return $delete;
    }
    function getLikes($photo_id){
       $this->mongo_db->where('photo_id' , $photo_id);
        $query = $this->mongo_db->get('likes_ph');
        return count($query);
    }
    function getLikesVid($video_id){
       $this->mongo_db->where('video_id' , $video_id);
        $query = $this->mongo_db->get('likes_vid');
        return count($query);
    }
    function getFavs($photo_id){
        $this->mongo_db->where('photo_id' , $photo_id);
        $query = $this->mongo_db->get('favorite_ph');
        return count($query);
    }
    function getFavsVid($video_id){
        $this->mongo_db->where('video_id' , $video_id);
        $query = $this->mongo_db->get('favorite_vid');
        return count($query);
    }
    function didLike($photo_id,$user_id){
        $this->mongo_db->where(array(
            'photo_id' => $photo_id,
            'user_id' => $user_id
        ));
        $query = $this->mongo_db->get('likes_ph');
        return count($query);
    }
    function didFav($photo_id,$user_id){
        $this->mongo_db->where(array(
            'photo_id' => $photo_id,
            'user_id' => $user_id
        ));
        $query = $this->mongo_db->get('favorite_ph');
        return count($query);
    }
    function didLikeVid($video_id,$user_id){
        $this->mongo_db->where(array(
            'video_id' => $video_id,
            'user_id' => $user_id
        ));
        $query = $this->mongo_db->get('likes_vid');
        return count($query);
    }
    function didFavVid($video_id,$user_id){
        $this->mongo_db->where(array(
            'video_id' => $video_id,
            'user_id' => $user_id
        ));
        $query = $this->mongo_db->get('favorite_vid');
        return count($query);
    }
    function deletePost($id,$type){
        $this->mongo_db->where($type.'_id' , $id);
        $query = $this->mongo_db->delete($type.'s');
        return $query;
    }
    function userLikes($user_id){
        
    }
    function userFavs($user_id){
        
    }
    function facebookOG($action,$url,$object){
        $fb_config = array(
                'appId' => '264489850252668',
                'secret' => '150cc65835eb81c20faa114175539758',
                'cookie' => true,
            );
            $this->load->library('facebook', $fb_config);
            $user = $this->facebook->getUser();
             
            if($user){

                $post = array(
                    $object=>$url
                    );
                   return $this->facebook->api('/me/leap_pk:'.$action,'POST',$post);

            }
    }
}
?>
